package cn.wolfcode.service.Impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.exception.BusinessException;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {


    private EmployeeMapper employeeMapper;
    @Autowired
    public void setEmployeeMapper(EmployeeMapper employeeMapper) {
        this.employeeMapper = employeeMapper;
    }

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);

    }

    @Override
    public void save(Employee employee) {

        employeeMapper.insert(employee);

    }

    @Override
    public void update(Employee employee) {

        employeeMapper.updateByPrimaryKey(employee);
    }

    @Override
    public Employee  get(Long id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        return employee;
    }

    @Override
    public List<Employee> listAll() {
        List<Employee> employees = employeeMapper.selectAll();
        return employees;
    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> list = employeeMapper.queryForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public Employee login(String username, String password) {
        if (!StringUtil.haslength(username)){
            throw new BusinessException("用户名不能为空");
        }
        if (!StringUtil.haslength(password)){
            throw new BusinessException("密码不能为空");
        }
         Employee employee=employeeMapper.selectByUsernameAndPassword(username,password);
                if (employee==null){
                    throw new BusinessException("请先登录");
                }
        return employee;
    }

    @Override
    public Workbook exportXls() {
        List<Employee> list = employeeMapper.selectAll();
        HSSFWorkbook workbook=new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("通讯录");
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("用户名");
        row.createCell(1).setCellValue("姓名");
        row.createCell(2).setCellValue("年龄");
        row.createCell(3).setCellValue("邮箱");
        for (int i=0;i<list.size();i++){
            row=sheet.createRow(i+1);
            Employee employee = list.get(i);
            row.createCell(0).setCellValue(employee.getUsername());
            row.createCell(1).setCellValue(employee.getName());

            row.createCell(2).setCellValue(employee.getAge());
            row.createCell(3).setCellValue(employee.getEmail());
            if (employee.getAge()!=null){
                row.createCell(2).setCellValue(employee.getAge());
            }
        }
        return workbook;
    }

    @Override
    public void importXls(MultipartFile file) throws Exception {
        HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
        HSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int i=0;i<=lastRowNum;i++){
            Row row = sheet.getRow(i);
            Employee employee = new Employee();
            String username = row.getCell(0).getStringCellValue();
                if (!StringUtil.haslength(username.trim())){
                    return;
                }
                employee.setUsername(row.getCell(0).getStringCellValue());
                employee.setName(row.getCell(1).getStringCellValue());
                employee.setAge((int)row.getCell(2).getNumericCellValue());
                employee.setEmail(row.getCell(3).getStringCellValue());
                employee.setAdmin(false);
                employee.setPassword("8888");
                employeeMapper.insert(employee);
        }
    }

    @Override
    public Employee selectByName(String username) {
        return employeeMapper.selectByName(username);
    }
}
